package org.flashamaphone.voices.waveformFactories
{
	import org.flashamaphone.voices.waveformFactories.WaveFactory;
	
	public class FMWaveform extends WaveFactory
	{
		/** multiplier for modulator (starts saw tooth wave, goes up to square) */
		public var modulatorMultiplier:Number;
		
		/** number of harmonic overtones */
		public var harmonicOvertones:int;
		
		public function FMWaveform(modulatorMultiplier:Number = 1, harmonicOvertones:int = 1)
		{
			this.modulatorMultiplier = modulatorMultiplier;
			this.harmonicOvertones = harmonicOvertones;
			super();
		}
		
		/**
		 * sample a sin wave at a phase
		 */
		public override function sample(phase:Number):Number {
			var sample:Number = 0;
			var harmonics:Number = phase;
			for (var c:int = 0; c < harmonicOvertones; c++ ) {
				sample += Math.sin( harmonics + Math.sin(harmonics * modulatorMultiplier));
				harmonics *= 2; // get next harmonic - one octave up
			}
			return sample
		}
	}
}